package cn.hs.lyxf.web.controller;

import cn.hs.lyxf.domain.SignedOrder;
import cn.hs.lyxf.domain.SignedOrderLog;
import cn.hs.lyxf.query.SignedOrderQuery;
import cn.hs.lyxf.result.JsonResult;
import cn.hs.lyxf.result.PageList;
import cn.hs.lyxf.service.ISignedOrderService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/signedOrder")
public class SignedOrderController {

    @Autowired
    public ISignedOrderService signedOrderService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody SignedOrder signedOrder) {
        if (signedOrder.getId() != null) {
            signedOrderService.updateById(signedOrder);
        } else {
            signedOrderService.insert(signedOrder);
        }
        return JsonResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
        signedOrderService.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id") Long id) {
        return JsonResult.success(signedOrderService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(signedOrderService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody SignedOrderQuery query) {
        Page<SignedOrder> page = new Page<SignedOrder>(query.getPage(), query.getRows());
        page = signedOrderService.selectPage(page);
        return JsonResult.success(new PageList<SignedOrder>(page.getTotal(), page.getRecords()));
    }

    /**
     * 指派员工进行签约洽谈
     */
    @RequestMapping(value = "/emp/signed/{empId}/{orderId}", method = RequestMethod.GET)
    public JsonResult empSigned(@PathVariable("empId") Long empId, @PathVariable("orderId") Long orderId) {
        return signedOrderService.empSigned(empId, orderId);
    }

    /**
     * 签约失败
     */
    @RequestMapping(value = "/signed/fail", method = RequestMethod.GET)
    public JsonResult signedFail(@RequestBody SignedOrderLog signedOrderLog) {
        return signedOrderService.signedFail(signedOrderLog);
    }

    /**
     * 签约成功
     */
    @RequestMapping(value = "/signed/prosperity", method = RequestMethod.GET)
    public JsonResult signedProsperity(@RequestBody SignedOrder signedOrder) throws Exception {
        return signedOrderService.signedProsperity(signedOrder);
    }

    /**
     * 根据公司id查订单信息
     */
    @RequestMapping(value = "/findByCompanyId/{companyId}", method = RequestMethod.GET)
    public SignedOrder findByCompanyId(@PathVariable("companyId") Long companyId) {
        EntityWrapper<SignedOrder> wrapper = new EntityWrapper<>();
        wrapper.eq("company_id", companyId);
        return signedOrderService.selectOne(wrapper);
    }
}